package apis

import (
	"github.com/gin-gonic/gin"
	"month_zh5/client/client"
	"month_zh5/common/proto/goods"
	"net/http"
)

func PageViewsTop(s *gin.Context) {
	top, err := client.PageViewsTop(s, &goods.PageViewsTopRequest{})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}

func Search(s *gin.Context) {
	var good struct {
		GoodsName string `json:"goods_name"`
		Userid    int64  `json:"userid"`
	}
	err := s.ShouldBind(&good)
	if err != nil {
		return
	}
	top, err := client.Search(s, &goods.SearchRequest{
		GoodsName: good.GoodsName,
		Userid:    good.Userid,
	})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}

func HighlightSearch(s *gin.Context) {
	var good struct {
		GoodsName string `json:"goods_name"`
		Userid    int64  `json:"userid"`
	}
	err := s.ShouldBind(&good)
	if err != nil {
		return
	}
	top, err := client.HighlightSearch(s, &goods.HighlightSearchRequest{
		GoodsName: good.GoodsName,
		Userid:    good.Userid,
	})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}

func SearchForRecords(s *gin.Context) {
	var good struct {
		GoodsName string `json:"goods_name"`
		Userid    int64  `json:"userid"`
	}
	err := s.ShouldBind(&good)
	if err != nil {
		return
	}
	top, err := client.SearchForRecords(s, &goods.SearchForRecordsRequest{
		GoodsName: good.GoodsName,
		Userid:    good.Userid,
	})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}

func ShoppingCart(s *gin.Context) {
	var shoppingCart struct {
		GoodsID   int64  `json:"goods_id"`
		GoodsName string `json:"goods_name"`
		Num       int64  `json:"num"`
		Userid    int64  `json:"userid"`
	}
	err := s.ShouldBind(&shoppingCart)
	if err != nil {
		return
	}
	top, err := client.ShoppingCart(s, &goods.ShoppingCartRequest{
		GoodsID:   shoppingCart.GoodsID,
		GoodsName: shoppingCart.GoodsName,
		Num:       shoppingCart.Num,
		Userid:    shoppingCart.Userid,
	})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}

func Comment(s *gin.Context) {
	var comment struct {
		GoodsID int64 `json:"goods_id"`
	}
	err := s.ShouldBind(&comment)
	if err != nil {
		return
	}
	top, err := client.Comment(s, &goods.CommentRequest{
		GoodsId: comment.GoodsID,
	})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}

func CreateComment(s *gin.Context) {
	var shoppingCart struct {
		GoodsId int64  `json:"goods_id"`
		UserID  int64  `json:"user_id"`
		Comment string `json:"comment"`
	}
	err := s.ShouldBind(&shoppingCart)
	if err != nil {
		return
	}
	top, err := client.CreateComment(s, &goods.CreateCommentRequest{})
	if err != nil {
		s.JSON(http.StatusInternalServerError, &gin.H{
			"code": http.StatusInternalServerError,
			"date": err.Error(),
		})
		return
	}
	s.JSON(http.StatusOK, &gin.H{
		"code": http.StatusOK,
		"date": top,
	})
}
